package com.lagou.hdfs.test_1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 第一种解法是使用reduce key相同的情况下，将map过程中自动排序完成之后在进行key相同
 * 在进行写入
 */
public class WorkSortDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        args = new String[]{"E:\\test_sort","E:\\test_sort_out"};

        // 进行配置
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 设置jar 加载的文件
        job.setJarByClass(WorkSortDriver.class);

        // 设置 map 和 reduce
        job.setMapperClass(WorkSortMapper.class);
        job.setReducerClass(WorkSortReduce.class);

        // 设置 map输出
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 设置 reduce 输出
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(LongWritable.class);

        //设置reduce的分组
        job.setGroupingComparatorClass(CustomGroupingComparator.class);

        job.setNumReduceTasks(1);

        // 设置输入 输出路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean b = job.waitForCompletion(true);

        System.exit(b ? 0 : 1);

    }

}
